﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;

namespace tsgl
{
    public partial class userPasswordEdit : Form
    {
        public userPasswordEdit(string text1,string text2)
        {
            InitializeComponent();
            this.label4.Visible = true;
            this.label4.Text = text1;
            this.label4.Visible = false;
            this.label5.Visible = true;
            this.label5.Text = text2;
            this.label5.Visible = false;
        }


        public string userPurview, userName;
        private void getUserList()
        {
            DataTable dt;
            userPurview = this.label4.Text;
            userName = this.label5.Text;
            if (userPurview == "系统管理员")
            {
                dt = bookDbClass.ExecuteDataTable("select 用户编号,用户名,密码,用户类型,启用日期,是否停用 from 用户信息");
                this.dataGridView1.DataSource = dt.DefaultView;
                this.txtOldPassword.DataBindings.Clear();
                this.txtOldPassword.DataBindings.Add("text",dt.DefaultView,"密码");
            }
            else
            {
                string str = "select 用户编号,用户名,密码,用户类型,启用日期,是否停用 from 用户信息 where 用户名 = '" + userName + "'";
                dt = bookDbClass.ExecuteDataTable(str); 
                this.dataGridView1.DataSource = dt.DefaultView;
                this.txtOldPassword.DataBindings.Clear();
                this.txtOldPassword.DataBindings.Add("text", dt.DefaultView, "密码");
            }
        }

        private void userPasswordEdit_Load(object sender, EventArgs e)
        {
            getUserList();
        }

        private void button1_Click(object sender, EventArgs e)
        {
                if (this.txtNewPassword.Text == "")
                {
                    MessageBox.Show("新密码不能为空！","提示信息");
                    this.txtNewPassword.Focus();
                }
                else if (txtNewPassword.Text.Trim() != txtConfirmPassword.Text.Trim())
                {
                    MessageBox.Show("新密码两次输入不一致，请重新输入新密码！");
                    txtConfirmPassword.Text = "";
                    txtNewPassword.Focus();
                }
                else
                {
                    try
                    {
                        string str=Convert.ToString(dataGridView1[1,dataGridView1.CurrentCell.RowIndex].Value).Trim();
                        bookDbClass.ExecuteNonQurey("update 用户信息 set 密码=@a where 用户名=@b", new SqlParameter("a", this.txtNewPassword.Text.Trim()),new SqlParameter("b",str));
                        MessageBox.Show("修改密码成功！", "提示信息");
                        getUserList();
                        this.txtNewPassword.Text = "";
                        this.txtConfirmPassword.Text = "";
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("修改密码失败！", "提示信息");
                    }
                }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void txtConfirmPassword_KeyDown(object sender, KeyEventArgs e)
        {
            this.AcceptButton = this.button1;
        }
    }
}